package com

import com.XferMgrProgress.waitForCompletion
import com.amazonaws.services.s3.internal.Mimetypes
import com.amazonaws.services.s3.model.CannedAccessControlList
import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.transfer.MultipleFileUpload
import com.amazonaws.services.s3.transfer.MyTransferManager
import org.slf4j.LoggerFactory
import java.io.File

/**
 * 上传队列
 */
class UploadQueue {
    /**
     * 原队列
     */
    private var list: List<File>? = null
    private var transferManager: MyTransferManager? = null
    private var consolePro: ConsoleProgressBar? = null

    fun setTransferManager(transferManager: MyTransferManager?): UploadQueue {
        this.transferManager = transferManager
        return this
    }

    fun startUpload(list: List<File>?) {
        this.list = list
        consolePro = ConsoleProgressBar(0, 100, 30)
        consolePro!!.startChar = "%n% "
        upload()
    }

    private fun upload() {
        println("上传数量 len = " + list!!.size)

        val xfer = transferManager!!.uploadFileList(
            Config.dataManage!!.packCmd.bucketName,
            Config.dataManage!!.packCmd.rootPath,
            File(Config.dataManage!!.packCmd.parentPath!!), list, { file: File, objectMetadata: ObjectMetadata ->
                objectMetadata.contentLength = file.length()
                objectMetadata.contentType = Mimetypes.getInstance().getMimetype(file)
            }, { null }, CannedAccessControlList.PublicRead
        )

        showMultiUploadProgress(xfer)
        // 等待完成
        waitForCompletion(xfer)
    }

    // Prints progress of a multiple file upload while waiting for it to finish.
    private fun showMultiUploadProgress(multi_upload: MultipleFileUpload) {
        // print the upload's human-readable description
        println(multi_upload.description)

        val sub_xfers = multi_upload.subTransfers

        var fileName: String
        do {
            fileName = ""
            for (u in sub_xfers) {
//                if (u.isDone()) {
//                    Transfer.TransferState xfer_state = u.getState();
//                    System.out.println("  " + xfer_state);
//                } else {
//                    TransferProgress progress = u.getProgress();
//                    double uploadPct = progress.getPercentTransferred();
//                    if (StringUtils.isEmpty(fileName)) {
//                        fileName = ((MyUploadImpl)u).getPutObjectRequest().getKey()
//                                +" "+((int)uploadPct)+"%";
//                    } else {
//                        fileName += ", "+((MyUploadImpl)u).getPutObjectRequest().getKey()
//                                +" "+((int)uploadPct)+"%";
//                    }
//                }
            }
            val progress = multi_upload.progress
            val pct = progress.percentTransferred

            //            consolePro.details = "  ["+fileName+"]";
            consolePro!!.details = ""
            consolePro!!.show(pct.toLong())
            // wait a bit before the next update.
            try {
                Thread.sleep(200)
            } catch (e: InterruptedException) {
                LoggerFactory.getLogger(javaClass).error(null, e)
                return
            }
        } while (!multi_upload.isDone)
        // print the final state of the transfer.
        val xfer_state = multi_upload.state
        consolePro!!.details = ""
        consolePro!!.show(100)
        println("\nMultipleFileUpload $xfer_state")
    }


    companion object {
        @JvmStatic
        fun create(): UploadQueue {
            return UploadQueue()
        }
    }
}
